<template>
  <el-form ref="ruleForm" :model="form" :rules="rules" label-position="top" rel="operateFrom">
    <el-form-item label="" v-if="operateType===1">
      确定要恢复这条流水需求吗？
    </el-form-item>
    <el-form-item label="" v-if="operateType===2">
      确定要清除这条流水需求吗？
    </el-form-item>
    <el-form-item label="" v-if="operateType===3">
      确定需要将该用户的流水清零吗？
    </el-form-item>
    <el-form-item prop="amount" label="输入调整流水金额（负数减，正数加）：" v-if="operateType===0">
      <el-input type="text" v-model="form.amount" autocomplete="off" size="medium" class="running-water-adjust"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input type="textarea" v-model="form.remark" autocomplete="off" size="medium "></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="save">确定</el-button>
      <el-button @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {
  memberRunningWaterAdjust, memberRunningWaterRemove, memberRunningWaterRecover, emptyFlow,
} from '@/services/api/member';
import form from '@/mixins/form';
import factory from '@/mixins/factory';

export default {
  mixins: [form, factory],
  props: ['operateType', 'orderNo', 'userId'],
  data() {
    return {
      limitFlag: true, // 防止多次提交
      adjustData: '',
      removeData: '',
      recoverData: '',
      form: {
        amount: 0,
        remark: undefined,
      },
      formFinal: {

      },
      rules: {
        remark: [
          { required: true, message: '请填写备注', trigger: 'blur' },
        ],
        amount: [
          { required: true, message: '请填写调整金额', trigger: 'blur' },
          { validator: factory.getValidator('createpointsLimit'), trigger: 'blur' },
        ],
      },
    };
  },
  mounted() {
    let apiName;
    switch (this.operateType) {
      case 0:
        apiName = memberRunningWaterAdjust;
        break;
      case 1:
        apiName = memberRunningWaterRecover;
        break;
      case 2:
        apiName = memberRunningWaterRemove;
        break;
      case 3:
        apiName = emptyFlow;
        break;
      default:
        break;
    }
    this.bindSave(apiName, 'saveForm', 'formFinal', {
      afterSuccess: this.afterSuccess,
    });
  },
  watch: {
    'form.remark': function (val) {
      if (!_.isUndefined(val)) {
        this.limitFlag = true;
      }
    },
    'form.amount': function (val) {
      const ruleExp = /^-?(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/;
      if (ruleExp.test(val)) {
        this.limitFlag = true;
      }
    },
  },
  methods: {
    save() {
      if (!this.limitFlag) {
        return;
      }
      this.limitFlag = false;
      switch (this.operateType) {
        case 0:
          // 调整流水
          this.formFinal.amount = Number(this.form.amount);
          this.formFinal.remark = this.form.remark;
          this.formFinal.userId = this.userId || this.$store.state.member.member_id;
          break;
        case 1:
          // 恢复流水
          this.formFinal.orderNo = this.orderNo;
          this.formFinal.remark = this.form.remark;
          break;
        case 2:
          // 清除流水
          this.formFinal.orderNo = this.orderNo;
          this.formFinal.remark = this.form.remark;
          break;
        case 3:
          // 流水清零
          this.formFinal.userId = this.userId || this.$store.state.member.member_id;
          this.formFinal.remark = this.form.remark;
          break;
        default:
          break;
      }
      this.doSave({ action: 'saveForm', form: 'ruleForm' });
    },
    afterSuccess() {
      this.$emit('getAllBind');
      this.cancel();
    },
    cancel() {
      this.$emit('input', false);
    },
  },
};
</script>

<style scoped>
  .running-water-adjust{
    width: 60%;
  }
</style>
